﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 激光
/// </summary>
public class Laser : Weapon {
	
	protected LineRenderer lineRenderer;//渲染器
	protected Color color;
	protected GameObject target;//攻击目标
	protected float targetScale;//目标放大倍数
	public bool isAttack = false;//是否是攻击状态
	public bool isInPool;
	protected float distance;//枪与目标之间的距离
	public bool isInterlock = false;
	public Vector3 interlockPos;
	public Vector3 endPosition;
	public int especialEffectId;

	protected GameObject gun;//发射激光的枪

	// Use this for initialization
	void Start () {
		gun = transform.parent.gameObject;
		lineRenderer = gameObject.GetComponent<LineRenderer>();
		lineRenderer.enabled = false;
	}
	
	// Update is called once per frame
	void Update () {
		
		Attacking ();
		
	}

	protected void Attacking()
	{
		// 刷新targetScale
		if(target != null)
		{
			targetScale = target.transform.localScale.x;
			if(target.transform.parent != null)
			{
				targetScale = target.transform.parent.transform.localScale.x * targetScale;
			}
			if(target.transform.Find("Shield(Clone)") != null)
			{
				target = target.transform.Find("Shield(Clone)").gameObject;
				targetScale = target.transform.localScale.x * targetScale;
			}
		}
		// 持续时间到了
		timer += Time.deltaTime;
		durationTime = 5f;
		if(timer > durationTime)
		{
			timer = 0;
			isAttack = false;
			gun.GetComponent<Gun>().isShow = isAttack;
			Destroy(gameObject);
			return;
		}
		// 目标死了
		if(target == null)
		{
			timer = 0;
			isAttack = false;
			gun.GetComponent<Gun>().isShow = isAttack;
			//			interlockPos = Vector3.zero;
			isInterlock = false;
			Destroy(gameObject);
			return;
		}
		if(target != null)
		{
			gun.GetComponent<Gun>().isShow = isAttack;
			distance = Vector3.Distance(transform.position, target.transform.position) - target.GetComponent<CircleCollider2D>().radius * target.transform.localScale.x;

			endPosition = GetEndPosition();
			if(distance <= attackDistance)
			{
				if (isInterlock == false) {
					lineRenderer.SetPosition (0, transform.parent.position + new Vector3 (0, 0, -1));
				} else 
				{
					if (target!=null) {
						if (interlockPos != Vector3.zero) {
							lineRenderer.SetPosition (0, interlockPos + new Vector3 (0, 0, -1));
						}
					} else 
					{
						timer = 0;
						isAttack = false;
						gun.GetComponent<Gun>().isShow = isAttack;
						//						interlockPos = Vector3.zero;
						isInterlock = false;
						Destroy(gameObject);
					}
				}
				// lineRenderer.SetPosition(1, target.transform.position);
				lineRenderer.SetPosition(1, endPosition);

				if(isAttack)
				{
					// 渲染射线，攻击目标
					lineRenderer.enabled = true;
					MakeDamage();
				}
			}
			// 超出攻击范围
			else
			{
				// lineRenderer.enabled = false;
				timer = 0;
				isAttack = false;
				target = null;
				gun.GetComponent<Gun>().isShow = isAttack;
				Destroy(gameObject);
			}
		}
	}


	// 得到渲染的终点
	protected Vector3 GetEndPosition()
	{
		// 得到向量反向
		Vector3 v = (target.transform.position - gun.transform.position).normalized;
		// 得到角度
		float angle = Vector3.Angle(Vector3.right, v) * Mathf.Deg2Rad;
		if(transform.position.y > target.transform.position.y)
		{
			angle = -angle;
		}
		// 护盾半径
		float r = target.GetComponent<CircleCollider2D>().radius * targetScale;
		// 计算坐标
		float x = Mathf.Cos(angle) * r;
		float y = Mathf.Sin(angle) * r;

		// 返回坐标
		return target.transform.position - new Vector3(x,y,0);
	}

	public void SetColor(Color color)
	{
		this.color = color;
	}


	protected void MakeDamage()
	{
		if(target.tag == "Hero" || target.tag == "Enemy")
		{
			target.SendMessage("TakeDamage", damage);
		}
	}

	public void SetTarget(GameObject target)
	{
		this.target = target;
	}

	public void SetGun(GameObject gun)
	{
		this.gun = gun;
	}
		
}
